﻿using System;
using System.Collections.Generic;
using ReAnimation.Domain;
using ReAnimation.Helpers.Common;
using ReAnimation.Helpers.WinForms;
using ReAnimation.MVP.ModelInterfaces;


namespace ReAnimation.MVP.Models
{
    public class MainModel: IMainModel
    {
        private IList<Molecule> _molecules = new List<Molecule>();
        private Molecule _currentMolecule = new Molecule();

        public Molecule CurrentMolecule
        {
            get { return _currentMolecule; }
            set 
            { 
                _currentMolecule = value;
                CurrentMoleculeChanged.Throw(this,EventArgs.Empty); 
            }
        }

        public IList<Molecule> Molecules
        {
            get { return new List<Molecule>(_molecules); }
        }

        public event EventHandler CurrentMoleculeChanged;
        public event EventHandler CurrentMoleculeStructureChanged;
        public event EventHandler<EventArg<Molecule>> MoleculeAdded;
        public event EventHandler<EventArg<Molecule>> MoleculeRemoved;
        public event EventHandler MoleculesCollectionChanged;

        public void CurrentMoleculeStructureWasModified()
        {
            CurrentMoleculeStructureChanged.Throw(this,EventArgs.Empty);    
        }

        public void AddMolecule(Molecule molecule)
        {
            if(_molecules.Contains(molecule)) return;

            _molecules.Add(molecule);

            MoleculeAdded.Throw(this, new EventArg<Molecule>(molecule));
            MoleculesCollectionChanged.Throw(this,EventArgs.Empty);
        }

        public void RemoveMolecule(Molecule molecule)
        {
            if (!_molecules.Contains(molecule)) return;

            _molecules.Remove(molecule);

            MoleculeRemoved.Throw(this,new EventArg<Molecule>(molecule));
            MoleculesCollectionChanged.Throw(this,EventArgs.Empty);
        }
    }
}